﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebTraderLiteCL;
using System.Data;

namespace WebTraderLite.Pages
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                userStocksSDS.SelectParameters.Add("userName", DbType.String, HttpContext.Current.User.Identity.Name);
                
                if (Request.QueryString["stock"] != null)
                {
                    lstUserStocks.SelectedValue = Request.QueryString["stock"].ToUpper();
                    
                    
                }
            }

            
        }
        protected void showConfirmSell(object sender, EventArgs e)
        {
            //Show confirm sell view
            MultiViewSellStock.ActiveViewIndex = 1;
            
            DBTransactions db = new DBTransactions();

            //Populate number of shares being sold
            TextBox tbNumShares = fvSellStock.FindControl("tbNumShares") as TextBox;
            Label lblNumShares = fvConfirmSell.FindControl("lblNumShares") as Label;
            lblNumShares.Text = tbNumShares.Text;
                        
            //Populate purchase price label
            Label lblPurchasePrice = fvConfirmSell.FindControl("lblPurchasePrice") as Label;
            float purchasePrice = db.retrievePurchasePrice(lstUserStocks.SelectedValue.ToString(), HttpContext.Current.User.Identity.Name);
            lblPurchasePrice.Text = purchasePrice.ToString("C");

            //Populate the total sale price
            Stock stk = new Stock(lstUserStocks.SelectedValue.ToString());
            float orderTotal = stk.Price * float.Parse(lblNumShares.Text);
            Label totPrice = fvConfirmSell.FindControl("lblTotPrice") as Label;
            totPrice.Text = orderTotal.ToString("C");
            

            //Populate users profit (or loss)
            Label lblProfit = fvConfirmSell.FindControl("lblProfitLoss") as Label;
            float profit = orderTotal - (purchasePrice * float.Parse(lblNumShares.Text));
            lblProfit.Text = profit.ToString("C");
            

        }
        
        protected void processSale(object sender, EventArgs e)
        {

            Label lblSymbol = fvConfirmSell.FindControl("lblStockSym") as Label;
            Stock stk = new Stock(lblSymbol.Text);
            Label lblNumShares = fvConfirmSell.FindControl("lblNumShares") as Label;
            float numSharesToSell = float.Parse(lblNumShares.Text);

           
            DBTransactions trans = new DBTransactions();
            User usr = new User(HttpContext.Current.User.Identity.Name);
            List<Int32> d=trans.getAvailableUserShare(usr,stk);
            if (d.Count > 0)
            {
                if (d.ElementAt(0) >= (Int32)numSharesToSell)
                {
                    //update userstock.
                    if (trans.updateStokAfterSell(usr, stk, numSharesToSell) == true)
                    {

                        //credit the balance;

                        trans.creditBalance(usr, stk, numSharesToSell);
                        trans.insertIntoHistory(usr, stk, numSharesToSell, "SELL");
                        trans.deductandAddbrokerFees(usr.getUserID());
                        //insert into history
                    }
                }
            }
            Response.Redirect("~/Pages/Transactions.aspx");
        }
        
        protected void cancelSell(object sender, EventArgs e)
        {
            //Redirect back to original buy stock page
            Response.Redirect("~/Pages/SellStock.aspx");
        }

        protected void lstUserStocks_SelectedIndexChanged(object sender, EventArgs e)
        {
            fvSellStock.DataBind();
            Label lblSharesOwn = fvSellStock.FindControl("lblSharesOwned") as Label;
            DBTransactions db = new DBTransactions();
            //we need to check whether it is null
            string sharesOwned = db.retrieveSharesOwned(lstUserStocks.SelectedValue.ToString(), HttpContext.Current.User.Identity.Name);
            lblSharesOwn.Text = sharesOwned;
        }
        

    }
}